local jw_juce = fk.CreateSkill {
    name = "jw_juce",
}
Fk:loadTranslationTable {
    ["jw_juce"] = "拒策",
    [":jw_juce"] = "摸牌阶段开始前，你可以改为摸X张牌，若如此做，本回合锦囊牌视为【杀】，回合内可使用【杀】的次数，手牌上限，至其他角色的距离加等量值，" ..
        "且存活的角色数：不小于势力数，你使用基本牌可以额外选择至多X个目标；不大于势力数，你使用基本牌无法被响应。(X为存活势力数-1，至少为1)",

    ["@jw_juce-turn"] = "拒策",
    ["#jw_juce-choose"] = "拒策：请选择此 %arg 的至多 %arg2 个目标",

    ["$jw_juce1"] = "还不跪下！",
    ["$jw_juce2"] = "这可是与生俱来的血统！",
}

jw_juce:addEffect(fk.EventPhaseChanging, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(jw_juce.name) and data.phase == Player.Draw and not data.skipped
    end,
    on_cost = function(self, event, target, player, data)
        local kingdom = {}
        for _, id in ipairs(player.room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        local n = math.max(1, #kingdom - 1) --至少为X，用math.max(x，常规值)
        return player.room:askToSkillInvoke(player,
            { skill_name = jw_juce.name, prompt = "拒策：你可以跳过摸牌阶段，改为摸" .. n .. "张牌并发动效果" })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        data.skipped = true
        local kingdom = {}
        for _, id in ipairs(player.room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        local n = math.max(1, #kingdom - 1)
        player:drawCards(n, jw_juce.name)
        room:setPlayerMark(player, "@jw_juce-turn", n)
        room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, n)
        room:addPlayerMark(player, MarkEnum.SlashResidue .. "-turn", n)
    end,
})

jw_juce:addEffect(fk.AfterCardTargetDeclared, { --指定目标时，多用于额外目标选择
    anim_type = "offensive",
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        local kingdom = {}
        for _, id in ipairs(player.room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        return target == player and #player.room.alive_players >= #kingdom and player:getMark("@jw_juce-turn") > 0 and
            data.card.trueName == "slash" and #data:getExtraTargets() > 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local kingdom = {}
        for _, id in ipairs(player.room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        local n = math.max(1, #kingdom - 1)
        local tos = room:askToChoosePlayers(player, {
            targets = data:getExtraTargets(),
            min_num = 0,
            max_num = n,
            prompt = "#jw_juce-choose:::" .. data.card:toLogString() .. ":" .. n,
            skill_name = jw_juce.name,
        })
        if #tos > 0 then
            room:sortByAction(tos)
            event:setCostData(self, { tos = tos })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        for _, p in ipairs(event:getCostData(self).tos) do
            data:addTarget(p)
        end
    end,
})

jw_juce:addEffect(fk.CardUsing, { --无法响应
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        local kingdom = {}
        for _, id in ipairs(player.room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        return target == player and player:getMark("@jw_juce-turn") > 0 and data.card and
            data.card.type == Card.TypeBasic and #player.room.alive_players <= #kingdom
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        data.disresponsiveList = table.simpleClone(player.room.players)
    end,
})

jw_juce:addEffect("distance", {
    correct_func = function(self, from, to)
        local kingdom = {}
        for _, id in ipairs(Fk:currentRoom().alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        local n = math.max(1, #kingdom - 1)
        if from:getMark("@jw_juce-turn") > 0 then
            return -n
        end
    end,
})

jw_juce:addEffect("filter", {
    anim_type = "offensive",
    card_filter = function(self, card, player)
        return player:getMark("@jw_juce-turn") > 0 and card.type == Card.TypeTrick and
            table.contains(player:getCardIds("h"), card.id)
    end,
    view_as = function(self, player, to_select)
        return Fk:cloneCard("slash", to_select.suit, to_select.number)
    end,
})

return jw_juce
